<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>TestAPI</title>
</head>

<body>
    <input type="text" id="input">
    <button type="submit" id="message">提交</button>

</body>
<script>
    //创建websocket对象
    let webSocket = new WebSocket("ws://127.0.0.1:8080/test");
    webSocket.onopen = function () {
        console.log("连接成功");
    }
    webSocket.onmessage = function (event) {
        console.log("收到消息" + event.data);
    }
    webSocket.onerror = function () {
        console.log("连接错误");
    }
    webSocket.onclose = function () {
        console.log("连接关闭");
    }

    //实现点击按钮后，通过websocket发送数据
    document.getElementById("message").onclick = function () {
        let input = document.getElementById("input").value;
        console.log("发送消息" + input);
        webSocket.send(input);
    }
</script>

</html>